﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;
using System.Xml;


namespace NUP
{
    public class val
    {
        public int valor { get; set; }
        public string desc { get; set; }
    }

    public partial class Acoes : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Populate series with random data
            Random random = new Random();
            for (int pointIndex = 0; pointIndex < 10; pointIndex++)
            {
                Chart1.Series["Series1"].Points.AddY(random.Next(45, 95));
                Chart1.Series["Series2"].Points.AddY(random.Next(5, 75));
            }

            // Set series chart type
            Chart1.Series["Series1"].ChartType = SeriesChartType.Line;
            Chart1.Series["Series2"].ChartType = SeriesChartType.Spline;

            // Set point labels
            Chart1.Series["Series1"].IsValueShownAsLabel = true;
            Chart1.Series["Series2"].IsValueShownAsLabel = true;

            // Enable X axis margin
            Chart1.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = true;

            // Enable 3D, and show data point marker lines
            Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
            Chart1.Series["Series1"]["ShowMarkerLines"] = "True";
            Chart1.Series["Series2"]["ShowMarkerLines"] = "True";


        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            XmlTextReader d = new XmlTextReader("http://www.bovespa.com.br/Cotacoes2000/formCotacoesMobile.asp?codsocemi=PETR4");

            while (d.Read())
            {
                if (d.Name == "PAPEL")
                {
                    double lastYValue = this.Chart2.Series[0].Points[this.Chart2.Series[0].Points.Count - 1].YValues[0];
                    double lastXValue = this.Chart2.Series[0].Points[this.Chart2.Series[0].Points.Count - 1].XValue + 1;

                    //lastYValue += Convert.ToDouble(d["QUANT_NEG"]) / 10000;
                    //this.Chart2.Series[0].Points.AddXY(lastXValue++, lastYValue);

                    //lastYValue = this.Chart2.Series[1].Points[this.Chart2.Series[1].Points.Count - 1].YValues[0];
                    //lastXValue = this.Chart2.Series[1].Points[this.Chart2.Series[1].Points.Count - 1].XValue + 1;

                    lastYValue = Convert.ToDouble(d["VALOR_ULTIMO"]);
                        this.Chart2.Series[0].Points.AddXY(lastXValue++, lastYValue);

                }
            }

            // Remove points from the left chart side if number of points exceeds 100.
            //while (this.Chart2.Series[0].Points.Count > 100)
            //{
            //    // Remove series points
            //    foreach (Series series in this.Chart2.Series)
            //    {
            //        series.Points.RemoveAt(0);
            //    }

            //}

            //// Adjust categorical scale
            //double axisMinimum = this.Chart2.Series[0].Points[0].XValue;
            //this.Chart2.ChartAreas[0].AxisX.Minimum = axisMinimum;
            //this.Chart2.ChartAreas[0].AxisX.Maximum = axisMinimum + 20;
        }
    }
}
